#include <stdio.h>

int big_small()
{
  union 
  {
    short a;
    char b;
  }c;

  c.a = 1;

  return (c.b == 1);
}

int big_small2()
{
  unsigned short test = 0x102;

  if ( *((unsigned char *) &test ) == 0x01)
    return 1;
  else
    return 0;
}

void main()
{
  if (big_small())
    printf("small endian.\n");
  else
    printf("big endian.\n");

  if (big_small2())
    printf("big endian.\n");
  else
    printf("small endian.\n");
}


#if 0

1: $rsp = (void *) 0x7fffffffe108
2: $rbp = (void *) 0x7fffffffe110
0000000000001169 <big_small>:
    1169:	f3 0f 1e fa          	endbr64 
    116d:	55                   	push   %rbp				// $rsp = (void *) 0x7fffffffe100
    116e:	48 89 e5             	mov    %rsp,%rbp		// rsp=rbp=0x7fffffffe100
    
	x/16b $rbp-0x2
	0x7fffffffe0fe: 0x00    0x00    0x10    0xe1    0xff    0xff    0xff    0x7f
	0x7fffffffe106: 0x00    0x00    0xe5    0x51    0x55    0x55    0x55    0x55

    1171:	66 c7 45 fe 01 00    	movw   $0x1,-0x2(%rbp)

	(gdb) x/16b $rbp-0x2
	0x7fffffffe0fe: 0x01    0x00    0x10    0xe1    0xff    0xff    0xff    0x7f
	0x7fffffffe106: 0x00    0x00    0xe5    0x51    0x55    0x55    0x55    0x55

	1177:	0f b6 45 fe          	movzbl -0x2(%rbp),%eax	// eax = 1
    117b:	3c 01                	cmp    $0x1,%al
    117d:	0f 94 c0             	sete   %al
    1180:	0f b6 c0             	movzbl %al,%eax
    1183:	5d                   	pop    %rbp
    1184:	c3                   	retq   

1: $rsp = (void *) 0x7fffffffe108
2: $rbp = (void *) 0x7fffffffe110
0000000000001185 <big_small2>:
    1185:	f3 0f 1e fa          	endbr64 
    1189:	55                   	push   %rbp
    118a:	48 89 e5             	mov    %rsp,%rbp
    118d:	48 83 ec 10          	sub    $0x10,%rsp
    1191:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    1198:	00 00 
    119a:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    119e:	31 c0                	xor    %eax,%eax

	(gdb) x/16b $rbp-0xa  
	0x7fffffffe0f6: 0x00    0x00    0x00    0x1b    0x6c    0x1d    0x78    0xd0
	0x7fffffffe0fe: 0x48    0x44    0x10    0xe1    0xff    0xff    0xff    0x7f

    11a0:	66 c7 45 f6 02 01    	movw   $0x102,-0xa(%rbp)

	(gdb) x/16b $rbp-0xa
	0x7fffffffe0f6: 0x02    0x01    0x00    0x1b    0x6c    0x1d    0x78    0xd0		// 高位存高地址，低位存低地址
	0x7fffffffe0fe: 0x48    0x44    0x10    0xe1    0xff    0xff    0xff    0x7f

    11a6:	48 8d 45 f6          	lea    -0xa(%rbp),%rax	// rax = 0x7fffffffe0f6
    11aa:	0f b6 00             	movzbl (%rax),%eax		// eax = 2
    11ad:	3c 01                	cmp    $0x1,%al
    11af:	75 07                	jne    11b8 <big_small2+0x33>
    11b1:	b8 01 00 00 00       	mov    $0x1,%eax
    11b6:	eb 05                	jmp    11bd <big_small2+0x38>
    11b8:	b8 00 00 00 00       	mov    $0x0,%eax
    11bd:	48 8b 55 f8          	mov    -0x8(%rbp),%rdx
    11c1:	64 48 33 14 25 28 00 	xor    %fs:0x28,%rdx
    11c8:	00 00 
    11ca:	74 05                	je     11d1 <big_small2+0x4c>
    11cc:	e8 9f fe ff ff       	callq  1070 <__stack_chk_fail@plt>
    11d1:	c9                   	leaveq 
    11d2:	c3                   	retq   

#endif
